package com.qf.flash.ui.business;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.qf.flash.R;
import com.qf.flash.event.SetRoleSuccessEvent;
import com.qf.flash.http.response.GameDetailResponse;
import com.qf.flash.mvp.contract.SetRoleContract;
import com.qf.flash.mvp.presenter.SetRolePresenter;
import com.qf.flash.ui.business.adapter.RoleSetItem;
import com.qf.flashlib.adapter.RVAdapter;
import com.qf.flashlib.app.FlashLib;
import com.qf.flashlib.widget.dialog.BaseDialog;
import com.qf.flashlib.widget.dialog.ViewHolder;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

/**
 * 创建日期：2020/3/15 on 14:01
 * 描述:
 * 作者:戴旭东
 */
public class SetRoleFragment extends BaseDialog implements View.OnClickListener, SetRoleContract.View {
    RecyclerView mRecyclerView;
    private RVAdapter<GameDetailResponse.UsersBean> mAdapter;
    private int type;
    private String orderId;
    private SetRolePresenter mSetRolePresenter;
    public static SetRoleFragment newInstarnce(String orderId, int type, ArrayList<GameDetailResponse.UsersBean> usersBeans) {
        SetRoleFragment fragment = new SetRoleFragment();
        Bundle b = new Bundle();
        b.putInt("type", type);
        b.putString("orderId", orderId);
        b.putSerializable("user", usersBeans);
        fragment.setArguments(b);
        return fragment;
    }

    @Override
    public int setUpLayoutId() {
        return R.layout.fragment_set_role;
    }

    @Override
    public void convertView(ViewHolder holder, BaseDialog dialog) {
        Bundle arguments = getArguments();
        if (arguments == null)
            return;
        type = arguments.getInt("type");
        orderId = arguments.getString("orderId");
        ArrayList<GameDetailResponse.UsersBean> usersBeans = (ArrayList<GameDetailResponse.UsersBean>) arguments.getSerializable("user");
        mSetRolePresenter = new SetRolePresenter();
        mSetRolePresenter.attachView(this);
        mRecyclerView = holder.getView(R.id.mRecyclerView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mAdapter = new RVAdapter<>(new RoleSetItem());
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemChildClickListener(new RVAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(View view, int position) {
                mAdapter.setSingleChosenPosition(position);
                mAdapter.notifyDataSetChanged();
            }
        });
        mAdapter.setDatas(usersBeans);

        holder.getView(R.id.tv_sure).setOnClickListener(this);
        holder.getView(R.id.tv_cancel).setOnClickListener(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mSetRolePresenter.detachView();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_cancel:
                dismiss();
                break;
            case R.id.tv_sure:
                setRole();
                break;
        }
    }

    private void setRole() {
        if (mAdapter.getSingleChosenPosition() != -1) {
            GameDetailResponse.UsersBean user = mAdapter.getData(mAdapter.getSingleChosenPosition());
            mSetRolePresenter.setRole(orderId, user.getUid(), type);
        }
    }

    @Override
    public void setRoleSuccess() {
        EventBus.getDefault().post(new SetRoleSuccessEvent());
        FlashLib.showToast("设置成功");
        dismissLoadingDialog();
        dismiss();
    }
}
